package beautiful.butterfly.distributed_task_processing_engine.task_manage.task_data_items_processor;

import lombok.Getter;
import lombok.Setter;

/**
 * 多个线程间进行通信(object,threadCount)
 */
public class ObjectLock
{

    private Object object = new Object();
    @Setter
    @Getter
    private int threadCount = 0;

    public ObjectLock()
    {
    }

    //synchronized
    public void objectWait() throws Exception
    {
        synchronized (object)
        {
            this.object.wait();
        }
    }

    public void objectNotifyAll() throws Exception
    {
        synchronized (object)
        {
            this.object.notifyAll();
        }
    }
    //count

    public void increaseThreadCount1()
    {
        synchronized (this)
        {
            threadCount = threadCount + 1;
        }
    }

    public void reduceThreadCount1()
    {
        synchronized (this)
        {
            threadCount = threadCount - 1;
        }
    }

    //
    public boolean reduceThreadCount1IfThreadCountNotEqual1AndReturnTheCurrentThreadIsNotLastThread()
    {
        synchronized (this)
        {
            if (this.threadCount == 1)
            {
                return false;
            } else
            {
                threadCount = threadCount - 1;
                return true;
            }
        }
    }


}
